<?php
/*
 * Copyright 2009 Surf Chen <http://www.surfchen.org>
 *
 * @License BSD 
 */
class httpreq {
    const AGENT_DEFAULT=-1;
    const AGENT_USER_DEFINE=0;

    const AGENT_FIREFOX_2_WIN=1;
    const AGENT_FIREFOX_3_0_WIN=2;
    const AGENT_FIREFOX_3_5_WIN=3;
    
    const AGENT_FIREFOX_2_MAC=4;
    const AGENT_FIREFOX_3_0_MAC=5;
    const AGENT_FIREFOX_3_5_MAC=6;

    const AGENT_FIREFOX_2_LINUX=7;
    const AGENT_FIREFOX_3_0_LINUX=8;
    const AGENT_FIREFOX_3_5_LINUX=9;
    
    const AGENT_IE_6=10;
    const AGENT_IE_7=11;
    const AGENT_IE_8=12;

    const METHOD_GET=1;
    const METHOD_POST=2;
    
    private $driver;

    private $method=self::METHOD_GET;
    private $headers=array();
    private $url;

    public function __construct() {
        $drivers_dir=dirname(__FILE__);
        if (function_exists('curl_init')) {
            require_once $drivers_dir.'/HttpDrivers/curl.php';
            $this->driver = new HttpDriverCurl;
        }
    }
    public function setUrl($url) {
        $this->url=$url;
    }
    public function setMethod($method) {
        $this->method=$method;
    }
    public function setHeader($key, $value) {
        $this->headers[$key]=$value;
    }
    public function send() {
        $this->driver->setUrl($this->url);
        $this->driver->setMethod($this->method);
        $headers=array();
        foreach ($this->headers as $k=>$v) {
            $headers[]=$k.': '.$v;
        }
        $this->driver->setHeaders($headers);
        return new httpres($this->driver->send());
    }
    public function actAs($agent) {
        switch ($agent) {
            case self::AGENT_FIREFOX_3_5_MAC;
                $ua='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5';
                break;
            default:
                $ua='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5';
        }
        $this->setHeader('User-Agent',$ua);
    }
}
class httpres {
    private $body;
    private $status;
    private $headers=array();
    public function __construct($http_bytes) {
        list($header,$body)=explode("\r\n\r\n",$http_bytes,2);
        $this->body=$body;
        $header_lines=explode("\r\n",$header);
        list($http_no,$this->status,$status_text)=explode(" ",$header_lines[0]);
        $this->status=(int)$this->status;
        array_shift($header_lines);
        foreach ($header_lines as $line) {
            list($key,$value)=explode(':',$line,2);
            $key=trim($key);
            $value=trim($value);
            $this->headers[$key]=$value;
        }
    }
    public function status() {
        return $this->status;
    }
    public function body() {
        return $this->body;
    }
    public function header($key) {
        return @$this->headers[$key];
    }
    public function htmlmeta($key) {
        if (preg_match('/<meta[^>]+'.preg_quote($key).'[^>]+>/i',$this->body,$m)
        && preg_match('/content[^=]*=\s*[\'"]?([^\'"]+)[\'"]?/',$m[0],$m2)
        ) {
           return $m2[1];
        }
        return false;
    }
    public function htmltitle() {
        if (preg_match('#<title>(.+)</title>#iUs',$this->body,$m)) {
            return $m[1];
        }
        return '';
    }
    public function charset() {
        $content_type=$this->header('Content-Type');
        if (
        ( stripos($content_type,'charset')===false
         && !($content_type=$this->htmlmeta('Content-Type'))
        ) || !preg_match('/charset[^=]*=[^=]*?([-a-z\d]+)/i',$content_type,$m)) {
            return 'utf-8';
        }
        return $m[1];
    }
}
